package com.zd.commodity.categoryMng.dao.impl;

import java.sql.SQLException;

import javax.annotation.Resource;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.stereotype.Repository;

import com.zd.commodity.categoryMng.dao.ICategoryDao;
import com.zd.commodity.categoryMng.entity.Category;
import com.zd.commodity.dao.impl.BaseDao;
@Repository
public class CategoryDao extends BaseDao<Category> implements ICategoryDao{
	@Resource(name = "sessionFactory")
	public void setSuperSessionFactory(SessionFactory sessionFactory){
		super.setSessionFactory(sessionFactory);
	}
	@Override
	public DetachedCriteria getDetachedCriteria(Category t1, Category t2,
			Object param) {
		DetachedCriteria dc=DetachedCriteria.forClass(Category.class);
		if(t1!=null){
			if(t1.getLevel()!=null && !t1.getLevel().equals("")){
				dc.add(Restrictions.eq("level",t1.getLevel()));
			}
			if(t1.getName()!=null && !t1.getName().equals("")){
				dc.add(Restrictions.like("name",t1.getName(), MatchMode.ANYWHERE));
			}
		}
		return dc;
	}
	
	//删除类别和品牌关联
	public void deleteCategoryBrand(final Long categoryId,final Long brandId){
		getHibernateTemplate().execute(new HibernateCallback<Integer>(){
			public Integer doInHibernate(Session session) throws HibernateException, SQLException {
				String sql="delete from m_category_brand where category_id="+ categoryId +" and brand_id="+brandId;
                session.createSQLQuery(sql).executeUpdate();
                return null;
            }
		});
	}

}
